/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package learnjava.googe.doc;

import com.google.gdata.client.DocumentQuery;
import com.google.gdata.client.docs.DocsService;
import com.google.gdata.data.MediaContent;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.docs.DocumentEntry;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;
import com.google.gdata.data.docs.FolderEntry;
import com.google.gdata.util.ServiceException;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author xddai
 */
public class GoogleDoc {

    private String feedPrefix;
    private String userName;
    private String password;
    private String appName;
    private DocsService docService;
    private Map<String, DocumentListEntry> folders;

    public GoogleDoc(String userName, String password) {
        this();
        this.userName = userName;
        this.password = password;
        try {
            this.docService = new DocsService(appName);
            this.docService.setUserCredentials(userName, password);
            initFolders();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public GoogleDoc() {
        this.feedPrefix = "http://docs.google.com/feeds/default/private/full/";
        this.appName = "JavaGoogleDocClientV1.0";
    }

    public DocumentListEntry getFolder(String folderName) {
        return folders.get(folderName);
    }

    public DocumentListEntry createFolder(String folderName) throws IOException, ServiceException {
        DocumentListEntry newEntry = getFolder(folderName);
        if (newEntry == null) {
            newEntry = new FolderEntry();
            newEntry.setTitle(new PlainTextConstruct(folderName));
            newEntry = docService.insert(new URL(feedPrefix), newEntry);
            if (newEntry != null) {
                folders.put(folderName, newEntry);
            }
        }
        return newEntry;
    }

    public DocumentListEntry createFolder(String folderName, String parent) throws IOException, ServiceException {
        DocumentListEntry newEntry, parentFolder;
        if (parent == null) {
            newEntry = createFolder(folderName);
        } else {
            parentFolder = createFolder(parent);
            newEntry = new FolderEntry();
            newEntry.setTitle(new PlainTextConstruct(folderName));
            String destFolderUri = ((MediaContent) parentFolder.getContent()).getUri();
            newEntry = docService.insert(new URL(destFolderUri), newEntry);
            if (newEntry != null) {
                folders.put(folderName, newEntry);
            }
        }
        return newEntry;
    }

    public DocumentListEntry uploadFile(File file, DocumentListEntry folder) throws MalformedURLException, IOException, ServiceException {
        DocumentListEntry newEntry = null;
        String destURi = ((MediaContent) folder.getContent()).getUri();
        String mineType = DocumentListEntry.MediaType.fromFileName(file.getName()).getMimeType();
        newEntry = new DocumentEntry();
        newEntry.setFile(file, mineType);
        newEntry.setTitle(new PlainTextConstruct(file.getName()));
        newEntry = docService.insert(new URL(destURi), newEntry);
        return newEntry;
    }

    public boolean hasFile(File file) throws MalformedURLException, IOException, ServiceException {
        boolean result = true;
        URL feedURL = new URL(feedPrefix);
        DocumentQuery query = new DocumentQuery(feedURL);
        query.setTitleQuery(file.getName());
        query.setTitleExact(true);
        query.setMaxResults(10);
        DocumentListFeed docs = docService.query(query, DocumentListFeed.class);
        if (docs.getEntries().size() == 0) {
            result = false;
        }
        return result;
    }

    private void initFolders() throws MalformedURLException, IOException, ServiceException {
        URL feedURL = new URL(feedPrefix + "-/folder");
        DocumentListFeed feed = docService.getFeed(feedURL, DocumentListFeed.class);
        folders = new HashMap<String, DocumentListEntry>();
        for (DocumentListEntry entry : feed.getEntries()) {
            folders.put(entry.getTitle().getPlainText(), entry);
        }
    }
}
